
package com.yuan.application;

/**
 * 所有activity的基类 
 * @author YUAN
 */
import com.yuan.graduate.R;
import com.yuan.graduate.R.anim;
import com.yuan.prefence.Constants;
import com.yuan.prefence.FileConstants;
import com.yuan.prefence.MsgKeys;
import com.yuan.prefence.PreferenceKeys;
import com.yuan.prefence.UrlConstants;
import com.yuan.unit.AsyncDataJobHandler;
import com.yuan.unit.BLog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;

public class BaseActivity extends Activity implements PreferenceKeys, MsgKeys, OnMyDestroyListener,
        Constants, UrlConstants, FileConstants {
    public static final int Activity_Default = 0x0;
    public static final int Activity_Splash = 0x1;
    protected int mActivityType = Activity_Default;
    public static int mActivityTime = -1;// Activity正处于的生命周期

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BLog.d(this.getClass().getName(), "onCreate");
        ApplicationCleaner.getInstance().registerApplicationDestroyListener(this);
        // 默认隐藏软键盘
        AsyncDataJobHandler.getInstance().bindContext(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        mActivityTime = 0;
    }

    @Override
    protected void onStart() {
        super.onStart();
        BLog.d(this.getClass().getName(), "onStart");
        mActivityTime = 1;
    }

    @Override
    protected void onResume() {
        super.onResume();
        BLog.d(this.getClass().getName(), "onResume");
        mActivityTime = 2;
    }

    @Override
    protected void onPause() {
        super.onPause();
        BLog.d(this.getClass().getName(), "onPause");
        mActivityTime = 3;
    }

    @Override
    protected void onStop() {
        super.onStop();
        BLog.d(this.getClass().getName(), "onStop");
        mActivityTime = 4;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        BLog.d(this.getClass().getName(), "onRestart");
        mActivityTime = 5;
    }

    /**
     * 当程序退出时执行的方法 对所有对象进行销毁
     */
    @Override
    public void onMyDestroy() {
        // TODO Auto-generated method stub
        finish();
        BLog.d(this.getClass().getName(), "onMyDestroy");
        ApplicationCleaner.getInstance().unRegisterApplicationDestroyListener(this);
        mActivityTime = -1;

    }

    /**
     * Activity生命周期的onDestroy()
     */
    @Override
    protected void onDestroy() {
        BLog.d(this.getClass().getName(), "onDestroy");
        ApplicationCleaner.getInstance().unRegisterApplicationDestroyListener(this);
        super.onDestroy();
        mActivityTime = -1;
    }

    /**
     * 启动intent调用此方法 加上了动画 统一处理
     * 
     * @author YUAN
     * @param mIntent
     */
    @Override
    public void startActivity(Intent intent)
    {
        super.startActivity(intent);
        overridePendingTransition(R.anim.page_in_left_right, R.anim.page_out_left_right);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        // TODO Auto-generated method stub
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.page_in_left_right, R.anim.page_out_left_right);
    }

    @Override
    public void finish() {
        super.finish();
        switch (mActivityType)
        {
            case Activity_Default:
                overridePendingTransition(R.anim.page_in_right_left,
                        R.anim.page_out_right_left);
                break;
            case Activity_Splash:
                overridePendingTransition(R.anim.empty, R.anim.activity_center_exit);
                break;
            default:
                break;
        }
        ApplicationCleaner.getInstance().unRegisterApplicationDestroyListener(this);
    }

}
